package com.blyat.xsoft.kernel.util;

import java.lang.reflect.Field;

/**
 * @author syh
 * @date 2019/8/26
 **/
public class ReflectionUtil {

    public static void setField(String fieldName, Object fieldValue, Object target) {
        try {
            Field field = getField(fieldName, target);
            field.set(target, fieldValue);
        } catch (Exception var4) {
            throw new RuntimeException("设置成员变量的值出错", var4);
        }
    }

    public static Object getFieldValue(String fieldName, Object target) {
        try {
            Field field = getField(fieldName, target);
            return field.get(target);
        } catch (NoSuchFieldException var3) {
            return null;
        } catch (IllegalAccessException var4) {
            return null;
        }
    }

    private static Field getField(String fieldName, Object target) throws NoSuchFieldException {
        Field field = target.getClass().getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }

        return field;
    }
}
